from arcgis.gis import GIS
from arcgis.raster import ImageryLayer

# 连接到ArcGIS
gis = GIS()

# 指定服务URL
service_url = "https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer"

# 创建影像图层
terrain_layer = ImageryLayer(service_url)

# 定义下载范围（根据需要调整）
extent = {
    "xmin": -180, "ymin": -90, "xmax": 180, "ymax": 90,
    "spatialReference": {"wkid": 4326}
}

# 导出数据为二进制格式
terrain_data = terrain_layer.export_image(bbox=extent, size=[1200, 600], f="image")

# 将二进制数据保存为本地文件
with open("./terrain_data.tif", "wb") as file:
    file.write(terrain_data)